#include "Menu.h"
#include "Pen.h"
#include <fstream>

Menu::Menu()
{
	m_ButtonPressedID = MENU_BUTTONS_SIZE;
	m_Type = MENU_TYPE;

	m_Color = D3DXCOLOR(255.0f,255.0f,255.0f,255.0f);
	
	m_CurrentState = IDLE;
	int Textures[MENU_STATE_SIZE] =	{MENU_TEXTURE, PRESSED_BUTTON_ONE, PRESSED_BUTTON_TWO, PRESSED_BUTTON_THREE};
	for(int i = 0;i < MENU_STATE_SIZE;++i)
	{
		m_pAllMenuStates[i] = Pen::Engine()->getTextureByIndex(Textures[i]);
		m_pAllMenuStates[i]->GetLevelDesc(0, &m_pDesc[i]);
		m_vCenter[i] = D3DXVECTOR3((float)(m_pDesc[i].Width / 2.0f),
								(float)(m_pDesc[i].Height / 2.0f),
								0.0f);
	}

	m_vPosition = D3DXVECTOR3((float)(SCREEN_WIDTH - m_vCenter[0].x),(float)(SCREEN_HEIGHT - m_vCenter[0].y),0.0f);

	D3DXMatrixTranslation(&m_Translate,
						m_vPosition.x,
						m_vPosition.y,
						m_vPosition.z);

	m_bPressed = false;
	SetButtonBoxes();
	SetTextInfo();
}

void Menu::Reset()
{
	m_ButtonPressedID = MENU_BUTTONS_SIZE;
}

//access' its file of info and sets all the data for the buttons
void Menu::SetButtonBoxes() {
	ifstream infile;
	infile.open("Resources/Bounding Box Info.txt");
	string DummyLine;
	getline(infile,DummyLine,'#');
	
	for(int i = 0;i < MENU_BUTTONS_SIZE;++i) {
		if(!infile.eof()) {
			getline(infile,DummyLine,'#');
			infile >> Boxes[i].top;
			infile >> Boxes[i].left;
			infile >> Boxes[i].bottom;
			infile >> Boxes[i].right;
		}
	}
	infile.close();
}

//Dependent on which button was pressed
void Menu::SetMenuState()
{
	switch(m_ButtonPressedID)
	{
	case BUTTON_ONE:
		m_CurrentState = PRESSED_ONE;
		break;
	case BUTTON_TWO:
		m_CurrentState = PRESSED_TWO;
		break;
	case BUTTON_THREE:
		m_CurrentState = PRESSED_THREE;
		break;
	default:
		m_CurrentState = IDLE;
			break;
	}
}

int Menu::GetState() {
	return (int)m_CurrentState;
}

int Menu::GetButtonPresesd()
{
	return m_ButtonPressedID;
}

//If 'any' button is pressed
bool Menu::ButtonPressed()
{
	if(m_ButtonPressedID == MENU_BUTTONS_SIZE)
		return false;
	return true;
}

//If specified button is pressed
bool Menu::ButtonPressed(int ID) {
	if(m_ButtonPressedID == ID)
		return true;
	return false;
}

//Checked against the mouse
bool Menu::CheckBoundingBoxes(COORD Pos) {
	for(int i = BUTTON_ONE;i < MENU_BUTTONS_SIZE;++i){
		if(Pos.X > Boxes[i].left && Pos.X < Boxes[i].right && Pos.Y > Boxes[i].top && Pos.Y < Boxes[i].bottom){
			m_ButtonPressedID = i;
			m_bPressed = true;
			return m_bPressed;
		}
	}
	m_bPressed = false;
	m_ButtonPressedID = MENU_BUTTONS_SIZE;
	return m_bPressed;
}

//access' its file of info and sets all the data for text buffers
void Menu::SetTextInfo()
{
	ifstream infile;
	infile.open("Resources/Menu Text Info.txt");
	string DummyLine;
	for(int i = 0;i < MENU_TEXT_LOC_SIZE;++i)
	{
		infile >> DummyLine;
		m_Buffers[i] << DummyLine;
		infile >> m_Displays[i].top;
		infile >> m_Displays[i].left;
		infile >> m_Displays[i].bottom;
		infile >> m_Displays[i].right;
	}
	infile.close();
}

//Can receive the health and gold of the character
void Menu::TranslateMessages()
{
	Messenger::Engine()->ReceiveMessages(this);
	if(m_Messages.getSize() != 0)
	{
		for(int i = 0;i < m_Messages.getSize();++i)
		{
			switch(m_Messages.get(i)->MessageType)
			{
			case STATS_HEALTH:
				m_Buffers[HEALTH_TEXT].str("");
				m_Buffers[HEALTH_TEXT] << "Health: " << (int)*m_Messages.get(i)->Info;
				Messenger::Engine()->ClearMessage(m_Messages.get(i));
				break;
			case STATS_EXP:
				m_Buffers[EXPERIENCE_TEXT].str("");
				m_Buffers[EXPERIENCE_TEXT] << "Gold: " << (int)*m_Messages.get(i)->Info;
				Messenger::Engine()->ClearMessage(m_Messages.get(i));
				break;
			default:
				Messenger::Engine()->ClearMessage(m_Messages.get(i));
				break;
			}
		}
		m_Messages.Clear();
	}
}

void* Menu::Update(void* Data)
{
	TranslateMessages();
	return NULL;
}